#include "ModelRepository.hpp"
#include <ecore/EcorePackage.hpp>
#include <ecorecpp/parser/parser.hpp>

using namespace ::ecorecpp;

std::auto_ptr< ModelRepository > ModelRepository::m_instance;

ModelRepository_ptr ModelRepository::_instance()
{
    if (m_instance.get() == NULL)
        m_instance.reset(new ModelRepository);
    return m_instance.get();
}

ModelRepository::ModelRepository()
{
}

ModelRepository::~ModelRepository()
{
}


::ecore::EObject_ptr ModelRepository::getByFilename(::ecore::EString const& _filename)
{
    try
    {
        ::ecore::EObject_ptr _m = m_by_filename.at(_filename);
        return _m;
    }catch (const std::out_of_range& e)
    {
        parser::parser _par;
        ::ecore::EObject_ptr _m = _par.load(_filename.c_str());
        return _m;
    }
}

void ModelRepository::addAlias(::ecore::EString const& _alias, ::ecore::EString const& _filename){
    ::ecore::EObject_ptr _m = getByFilename(_filename);
    putByFilename(_alias, _m);
    
}

void ModelRepository::putByFilename(::ecore::EString const& _filename, ::ecore::EObject_ptr _eobj)
{
    m_by_filename[_filename] = _eobj;
}
